import { Component, Inject } from '@angular/core';
import { App, IonicPage, NavController, ViewController } from 'ionic-angular';
import { Keyboard } from '@ionic-native/keyboard';
import { Storage } from '@ionic/storage';

import { Login } from '../../providers/entity/user.model';
import * as PageConstant from '../../constant/pageConstant';
import { Subscription } from 'rxjs/Subscription';

/**
 * 登录
 */
@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})
export class LoginPage {

  // 隐藏logo
  private hideLogo: boolean= false;
  // 监听
  private KeyboardShow: Subscription;
  private KeyboardHide: Subscription;
  private loginSubscription: Subscription;

  // 登录
  private login: Login= {
    username: '',
    password: ''
  };

  // 是否显示密码
  private switchover: any = {
    type: 'password',
    status: true
  };

  constructor(
    private appCtrl: App,
    private navCtrl: NavController,
    public viewCtrl: ViewController,
    private keyboard: Keyboard,
    public storage: Storage,
    @Inject('auth') private authService,
  )
  {
  }
  /*** 页面进入
   * */
  ionViewWillEnter()
  {
    this.setStorageLogin();
    this.initSubscription();
  }
  /*** 页面进入就保存标识,(用于修改密码路由判断)
   * 用于判断该viewCtrl是否激活,
  * */
  private setStorageLogin()
  {
    this.storage.get(PageConstant.LoginPage)
      .then(data => {
        if(!data) return;
        this.storage.set(PageConstant.LoginPage,'enter');
    });
  }
  /*** 页面离开
   * */
  ionViewWillLeave()
  {
    this.removeSubscription();
  }

  /*** 初始化监听
   * */
  private initSubscription(): void
  {
    this.KeyboardShow= this.keyboard.onKeyboardShow().subscribe(data=>{
      if(!data) return;
      this.hideLogo= true;
    });

    this.KeyboardHide= this.keyboard.onKeyboardHide().subscribe(data=>{
      if(!data) return;
      this.hideLogo= false;
    });

    this.loginSubscription = this.authService.getAuth()
      .filter(auth=> auth.login!== null)
      .map(auth=> auth.login.username)
      .subscribe(username=> {
        this.login.username= username || '';
      });
  }
  /*** 移除监听
  * */
  private removeSubscription(): void
  {
    if(typeof this.KeyboardShow !== 'undefined')
      this.KeyboardShow.unsubscribe();

    if(typeof this.KeyboardHide !== 'undefined')
      this.KeyboardHide.unsubscribe();

    if(typeof this.loginSubscription !== 'undefined')
      this.loginSubscription.unsubscribe();
  }

  /*** 清除登录名
  * */
  public clearUserName(): void
  {
    this.login.username='';
  }

  /*** 切换密码显示方式
  * */
  public switchoverPassword(obj: any): void
  {
    if(!obj.type) return;
    this.switchover = {
      type: obj.type==='text'? 'password': 'text',
      status: !obj.status
    }

  }

  /*** 执行登录操作
  * */
  public executeLogin(): void
  {
    this.authService.loginWithCredentials(this.login,this.appCtrl, this.viewCtrl);
  }

  /***
   * 打开注册
   * */
  public doRegister(): void
  {
    this.navCtrl.push(PageConstant.RegisterPage);
  }

  /***
   * 打开忘记密码
   * */
  public doForget(): void
  {
    this.navCtrl.push(PageConstant.ForgetPage);
  }

}
